package org.dyndns.ihippo.framework.osgi.spring.dm.petstore.util;

import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.springframework.osgi.context.BundleContextAware;

/**
 * Web资源管理器，用于向HttpService注册和注销Web资源
 * 
 * @author chris
 * 
 */
public class WebResourceMgr implements BundleContextAware, ServiceListener{
	
	private BundleContext bundleContext;
	
	private ServiceReference ref;
	
	private WebConfigMgr webConfigMgr;
	
	private String relativeResourcePath;
	
	private String name;
	
	
	@Override
	public void serviceChanged(ServiceEvent event) {
		switch (event.getType()) {
		case ServiceEvent.REGISTERED:
			registerWebResource();
			break;
			
		case ServiceEvent.UNREGISTERING:
			unregisterWebResource();
			break;
		}
		
	}
	
	@Override
	public void setBundleContext(BundleContext context) {
		bundleContext = context;
	}
	
	
	
	
	public void start() throws InvalidSyntaxException {
		registerWebResource();
		this.bundleContext.addServiceListener(this, "(objectClass=" + HttpService.class.getName() + ")");
	}
	
	
	/**
	 * 注册资源
	 */
	private void registerWebResource() {
		HttpService service = getHttpService();
		
		//TODO 查看API
		try {
			if(null != service)
				service.registerResources(getResourceAlias(), getName(), null);
		} catch (NamespaceException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 注销资源
	 */
	private void unregisterWebResource() {
		try {
			HttpService httpService = getHttpService();
			if (null != httpService) {
				httpService.unregister(getResourceAlias());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	private HttpService getHttpService() {
		//查找HttpService
		if(ref == null) 
			ref = bundleContext.getServiceReference(HttpService.class.getName());
		
		//返回服务引用
		if(ref != null)
			return (HttpService) bundleContext.getService(ref);
		
		return null;
	}
	
	private String getResourceAlias() {
		return webConfigMgr.getResourcePath() + "/" + getRelativeResourcePath();
	}

	
	
	
	
	
	
	
	
	
	
	
	
	public ServiceReference getRef() {
		return ref;
	}

	public void setRef(ServiceReference ref) {
		this.ref = ref;
	}

	public WebConfigMgr getWebConfigMgr() {
		return webConfigMgr;
	}

	public void setWebConfigMgr(WebConfigMgr webConfigMgr) {
		this.webConfigMgr = webConfigMgr;
	}

	public String getRelativeResourcePath() {
		return relativeResourcePath;
	}

	public void setRelativeResourcePath(String relativeResourcePath) {
		this.relativeResourcePath = relativeResourcePath;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public BundleContext getBundleContext() {
		return bundleContext;
	}
}
